package com.gym.Reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ParsonTest {
	public static void main(String[] args) {
		String name = "com.gym.Reflection.Parson";
		try {
			//通过类名加载实列
			Class c = Class.forName(name);
			//获取指定参数的构造方法
			Constructor constructor = c.getConstructor(String.class,Integer.class);
			//通过构造方法产生实例
			/*Parson p=(Parson) constructor.newInstance("张三",20);
			System.err.println(p.toString());
			System.out.println(c.getInterfaces());*/
			Object obj = constructor.newInstance("abc",20);
			Field field =c.getDeclaredField("name");
			field.setAccessible(true);
			field.set(obj, "一明");
			Method method = c.getMethod("getName");
			System.out.println(field.get(obj));
			System.err.println(method.invoke(obj));
			
			/*//通过类名加载实列
			Class c = Class.forName(name);
			//获取指定参数的构造方法
			Constructor constructor = c.getConstructor(String.class,Integer.class);
			Object obj = constructor.newInstance("abc",20);
			//获取取值方法
			Method method = c.getMethod("getName");
			//获取赋值的方法，set的时候，带参数
			Method method2 = c.getMethod("setName",String.class);
			//调用方法设置值
			System.err.println(method2.invoke(obj, "张三"));
			//调用方法取值
			System.out.println(method.invoke(obj));
			
			
			//获取私有的方法
			Method method3 = c.getDeclaredMethod("doStatic");
			//设置为可见的，调用私有的方法
			method3.setAccessible(true);
			//使用方法对象调用方法
			method3.invoke(obj);*/
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
}
